/*
 * Copyright 2020 tu.cn All right reserved. This software is the
 * confidential and proprietary information of tu.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Tu.cn
 */
package com.onedec.schedule.controller;

import com.onedec.schedule.entity.OnedecSchedule;
import com.onedec.schedule.service.ScheduleService;
import com.onedec.utils.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 31014
 * @date 2020/7/9 19:40
 */
@RestController
@RequestMapping("schedule")
public class ScheduleController {
    @Resource
    private ScheduleService scheduleService;

    @PostMapping
    public ResponseEntity<Void> insertSchedule(String date, String value, String title) throws ParseException {
        OnedecSchedule schedule = new OnedecSchedule();
        schedule.setScheduleContent(value);
        final SimpleDateFormat dateFormat = new SimpleDateFormat("yy-MM-dd");
        final Date time = dateFormat.parse(date);
        schedule.setScheduleStartTime(time);
        schedule.setScheduleName(title);
        schedule.setScheduleBak1("3");
        scheduleService.insertSchedule(schedule);
        return ResponseEntity.SUCCESS;
    }

    @GetMapping
    public ResponseEntity<List<Map<String, Object>>> selectSchedule(String empId) {
        final List<OnedecSchedule> schedules = scheduleService.selectSchedule("3");
        final List<Map<String, Object>> maps = new ArrayList<>();
        schedules.forEach(e -> {
            final Map<String, Object> map = new HashMap<>();
            map.put("time", e.getScheduleStartTime());
            map.put("value", e.getScheduleContent());
            map.put("title", e.getScheduleName());
            maps.add(map);
        });
        return new ResponseEntity<List<Map<String, Object>>>(maps);
    }
}